package com.qingmu.club.minaapi.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.plus.entity.Activity;
import com.qingmu.club.minaapi.plus.entity.ActivityCenter;
import com.qingmu.club.minaapi.plus.entity.ClubShop;
import com.qingmu.club.minaapi.plus.entity.OrderInfo;
import com.qingmu.club.minaapi.plus.service.ActivityCenterService;
import com.qingmu.club.minaapi.plus.service.ActivityService;
import com.qingmu.club.minaapi.plus.service.ClubShopService;
import com.qingmu.club.minaapi.utils.ConvertUtil;
import com.qingmu.club.minaapi.utils.FontUtil;
import com.qingmu.club.minaapi.utils.KeyValueDefine;
import com.qingmu.club.minaapi.utils.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 活动中心主表 前端控制器
 * </p>
 *
 * @author duke
 * @since 2019-08-20
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(value = "活动中心相关(会员端小程序)", description = "活动中心相关(会员端小程序)")
@RequestMapping("/member/activityCenter")
public class MemberActivityCenterController {

    @Autowired
    ActivityCenterService activityCenterService;

    @Autowired
    ActivityService activityService;

    @Autowired
    ClubShopService clubShopService;

    @RequestMapping(value = "/select", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "活动中心")
    public ResponseEntity select(
            @ApiParam(required = true, value = "门店id") @RequestParam(required = true) String shopId
    ) {
        QueryWrapper<ActivityCenter> qw = new QueryWrapper<>();
        qw.eq(ActivityCenter.SHOP_ID, shopId)
                .eq(ActivityCenter.ACTIVITY_HEAT,0)
                .eq(ActivityCenter.STATUS, KeyValueDefine.ONLINE_STATUS)
                .orderByDesc(ActivityCenter.START_TIME);
        List<ActivityCenter> activityList1  = activityCenterService.list(qw);

        List<ActivityCenter> activityCenterShopActivity = new ArrayList<>();//活动中心门店活动
        List<ActivityCenter> activityCenterLastActivity = new ArrayList<>();//活动中心往期活动

//        List<HashMap<String,Object>> list = new ArrayList<>();
//        Long presentTime = new Date().getTime();
//        Integer type = 0;
        Long presentTime = new Date().getTime();
        for (ActivityCenter activityCenter : activityList1){
            Long endTime = activityCenter.getEndTime().getTime();
            if (endTime > presentTime){
                activityCenterShopActivity.add(activityCenter);
            }
        }

//        QueryWrapper<ActivityCenter> qw2 = new QueryWrapper<>();
//        qw2.lambda().eq(ActivityCenter::getShopId,shopId).eq(ActivityCenter::getActivityHeat,0).eq(ActivityCenter::getStatus,1)
//                .eq(ActivityCenter::getOver,0).orderByDesc(ActivityCenter::getStartTime);
//        List<ActivityCenter> activityList2  = activityCenterService.list(qw2);

        for (ActivityCenter activityCenter : activityList1){
            Long endTime = activityCenter.getEndTime().getTime();
            if (endTime < presentTime){
                activityCenterLastActivity.add(activityCenter);
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("activityCenterShopActivity", activityCenterShopActivity);//活动中心门店活动
        map.put("activityCenterLastActivity", activityCenterLastActivity);//活动中心往期活动
        return ResponseObject.success(map);
    }

    @RequestMapping(value = "/selectShop", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "热门活动")
    public ResponseEntity selectShop(
            @ApiParam(required = true, value = "门店id") @RequestParam(required = true) String shopId
    ) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.eq(Activity.ACTIVITY_TYPE,KeyValueDefine.HOT_CLUB_ACTIVITY).eq(Activity.STATUS,KeyValueDefine.ONLINE_STATUS);
        Activity activity = this.activityService.getOne(qw);

        ClubShop clubShop = this.clubShopService.getById(shopId);
        if (clubShop == null){
            return ResponseObject.error(555,"门店找不到信息");
        }
        QueryWrapper<ActivityCenter> qw2 = new QueryWrapper<>();
        qw2.eq(ActivityCenter.CLUB_ID,clubShop.getClubId())
                .eq(ActivityCenter.ACTIVITY_HEAT,1)
                .eq(ActivityCenter.STATUS, KeyValueDefine.ONLINE_STATUS);//上架状态
        List<ActivityCenter> activityCenterList  = activityCenterService.list(qw2);

        HashMap<String, Object> map = new HashMap<>();
        map.put("activity", activity);
        map.put("activityCenterList", activityCenterList);

        return ResponseObject.success(map);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询活动详情")
    public ResponseEntity getById(
            @ApiParam(required = true, value = "活动id") @RequestParam(required = true) String activityId
    ) {
        ActivityCenter activityCenter  = activityCenterService.getById(activityId);
        if(activityCenter==null){
            return ResponseObject.error(601,"活动不存在");
        }
        return ResponseObject.success(activityCenter);
    }

}
